/**
 * 角色操作类
 * @author lld
 * @version 1.0
 */
package com.douyu.water.core.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.douyu.water.core.DwzResult;
import com.douyu.water.core.PagePara;
import com.douyu.water.core.model.Menu;
import com.douyu.water.core.model.Role;
import com.douyu.water.core.model.RoleMenuKey;
import com.douyu.water.core.service.RoleService;

@Controller
@RequestMapping("/role/*")
public class RoleController {

	@Resource
	private RoleService roleService;
	
	/**
	 * 检索角色列表
	 * @param request
	 * @param role Role  
	 *        -角色
	 * @param page PagePara   
	 *        -分页
	 * @return
	 */
	@RequestMapping("/select")
	public String select(HttpServletRequest request,Role role,PagePara page)
	{
		String result = "WEB-INF/jsp/role/list";
		try
		{
			List<Role> list = roleService.select(role, page);
			request.setAttribute("list", list);
			request.setAttribute("role", role);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 添加页面
	 * @param req
	 * @return
	 */
	
	@RequestMapping("/add")
	public String add(HttpServletRequest req)
	{
		return "WEB-INF/jsp/role/add";
	}
	
	/**
	 * 记录近数据库
	 * @param req
	 * @param role Role  
	 *        -角色
	 * @return String
	 */
	@RequestMapping("/insert")
	public String insert(HttpServletRequest req,Role role)
	{
		DwzResult dwzResult = new DwzResult();
		req.setAttribute("dwzResult",dwzResult);
		try
		{
			int flag = roleService.insert(role);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("添加失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("添加成功");
				dwzResult.setNavTabId("roleList");
				dwzResult.setCloseCurrent(true);
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("角色名称重复，添加失败");
				break;
			}			
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	
	/**
	 * 修改页面
	 * @param request
	 * @param ucode String  
	 *        -角色主键
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,String ucode)
	{
		String result = "WEB-INF/jsp/role/edit";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			Role role = roleService.view(ucode);
			if(role != null)
			{
				request.setAttribute("rol", role);				
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取对象");
				return DwzResult.URL;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	
	/**
	 * 修改角色
	 * @param request
	 * @param role Role  
	 *        -角色
	 * @return String
	 */
	@RequestMapping("/update")
	public String update(HttpServletRequest request,Role role)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = roleService.update(role);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("更新失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("更新成功");
				dwzResult.setCloseCurrent(true);
				dwzResult.setNavTabId("roleList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("名称重复，更新失败");
				break;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	
	/**
	 * 删除角色
	 * @param request
	 * @param ucode String   
	 *        -角色编号
	 * @return  String
	 */
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request,String ucode)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = roleService.delete(ucode);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("删除失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("删除成功");
				dwzResult.setCloseCurrent(false);
				dwzResult.setNavTabId("roleList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("正在使用，无法删除");
				break;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	
	/**
	 * 检索系统角色
	 * @param request
	 * @param ucode String  
	 *        -角色编号
	 * @return String
	 */
	@RequestMapping("/selectAuth")
	public String selectAuth(HttpServletRequest request,String ucode)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		String result ="WEB-INF/jsp/role/auth";
		try
		{
			List<Menu> list = roleService.selectMenu(ucode);
			request.setAttribute("menu", list);
			request.setAttribute("roleCode", ucode);
		}
		catch(Exception e){
			e.printStackTrace();
			
		}
		return result;
	}
	
	/**
	 * 修改系统权限
	 * **/
	@RequestMapping("/insertAuth")
	public String insertAuth(HttpServletRequest request,RoleMenuKey rm)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = roleService.updateAuth(rm);
			if(flag > 0)
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("设置权限完成");
				dwzResult.setCloseCurrent(true);
				dwzResult.setNavTabId("roleList");
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("设置权限失败");
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
}
